
from .routers import app_question_ganerate_router
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
import logging
from fastapi import FastAPI, Request, HTTPException
import time
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()

# 配置CORS（根据实际需求调整） 跨域
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有域名访问
    allow_credentials=True,  # 允许携带身份验证信息（如 cookies）
    allow_methods=["*"],  # 允许所有 HTTP 方法
    allow_headers=["*"],  # 允许所有 HTTP 头
)

app.include_router(app_question_ganerate_router)

@app.get("/")
async def read_root():
    return {"message": "Welcome to the llm API"}

@app.get("/test")
def valid_sync():
    time.sleep(10)  # 在 FastAPI 线程池中运行
    return {"valid": True}

# 配置日志
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
    # 打印出请求和错误的详细信息
    print(f"Request: {request}")
    print(f"Validation error: {exc}")
    return JSONResponse(
        status_code=422,
        content={"detail": exc.errors()},
    )

# 启动服务
if __name__ == "__main__":
    import uvicorn
    #uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)
   # uvicorn.run("app.main:app", host="127.0.0.1", port=8081,reload=True,workers=4)
    uvicorn.run("app.main:app", host="127.0.0.1", port=8081,reload=True)